using QFramework;
using QGameFramework.Enums;
using QGameFramework.Models;
using UnityEngine;

namespace QGameFramework.QInternet
{
    public class InternetComponent : BaseComponent
    {
#if NETWORK_TCP || NETWORK_UDP
        [Header("Clinet or Server")] public InternetEnum m_Internet;
#endif

#if NETWORK_TCP
        [HideInInspector] public TCPClient m_Client;
        [HideInInspector] public TCPServer m_Server;
#endif

#if NETWORK_UDP
        [HideInInspector] public UDPClient m_Client;
        [HideInInspector] public UDPServer m_Server;
#endif

        /// <summary>
        /// 游戏配置
        /// </summary>
        private IGameConfig m_GameConfig;

        public override void OnInit()
        {
            LogKit.I($"【{this.GetType().Name}】 is Inited");

            m_GameConfig = this.GetModel<IGameConfig>();

#if NETWORK_TCP
            switch (m_Internet)
            {
                case InternetEnum.Client:
                    m_Client = new GameObject().Parent(this).AddComponent<TCPClient>().Name("Client");
                    m_Client.OnInit();

                    //TODO 连接服务器
                    m_Client.OnConnectedToServer(m_GameConfig.IpAdr, m_GameConfig.Port);
                    break;
                case InternetEnum.Server:
                    m_Server = new GameObject().Parent(this).AddComponent<TCPServer>().Name("Server");
                    m_Server.OnInit();
                    Debug.Log(m_GameConfig.Port);
                    //TODO 打开服务器
                    m_Server.StartServer(m_GameConfig.Port);
                    break;
            }
#endif

#if NETWORK_UDP
            switch (m_Internet)
            {
                case InternetEnum.Client:
                    m_Client = new GameObject().Parent(this).AddComponent<UDPClient>().Name("Client");
                    m_Client.OnInit();

                    //TODO 连接服务器
                    m_Client.OnConnectedToServer(m_GameConfig.IpAdr, m_GameConfig.Port);

                    break;
                case InternetEnum.Server:
                    m_Server = new GameObject().Parent(this).AddComponent<UDPServer>().Name("Server");
                    m_Server.OnInit();

                    //TODO 打开服务器
                    m_Server.StartServer(m_GameConfig.Port);

                    break;
            }
#endif
        }
    }
}